<?
class BBCode{
    static function parseBBCode($text, $direct = false)
    {
        $old_text = $text;

        $text = stripslashes($text);

        //  $text = htmlspecialchars($text);
        //$text = nl2br($text);
        // First: If there isn't a "[" and a "]" in the message, don't bother.
        if (strpos($old_text, "[") !== false && strpos($old_text, "]") !== false) {
            $arr_replace = array(
                '/\[font\=(.*?)\](.+?)\[\/font\]/is' => '<span style="font-family: $1;">$2</span>',
                '/\[font\=(.*?)\](\s*)\[\/font\]/is' => '',
                '/\[size\=(.*?)\](.+?)\[\/size\]/is' => '<span style="font-size: $1;">$2</span>',
                '/\[size\=(.*?)\](\s*)\[\/size\]/is' => '',
                '/\[color\=(.*?)\](.+?)\[\/color\]/is' => '<span style="color: $1;">$2</span>',
                '/\[color\=(.*?)\](\s*)\[\/color\]/is' => '',
                //'/\[img\](.+?)\[\/img\]/is'     			=>  '<img src="$1" alt="Picture" border="0"/>',
                '/\[img\](.+?)\[\/img\]/is' => '',
                '/\[img\](\s*)\[\/img\]/is' => '',
                /* "/\\[b\\](.+?)\[\/b\]/is"					=>	'<b>$1</b>',
                  "/\\[b\\](\s*)\[\/b\]/is"					=>	'',
                  "/\\[i\\](.+?)\[\/i\]/is"					=>	'<i>$1</i>',
                  "/\\[i\\](\s*)\[\/i\]/is"					=>	'',
                  "/\\[u\\](.+?)\[\/u\]/is"					=>	'<u>$1</u>',
                  "/\\[u\\](\s*)\[\/u\]/is"					=>	'', */

                "/\\[b\\]/is" => '<b>',
                "/\\[\/b\]/is" => '</b>',
                "/\\[i\\]/is" => '<i>',
                "/\\[\/i\]/is" => '</i>',
                "/\\[u\\]/is" => '<u>',
                "/\\[\/u\]/is" => '</u>',
//          '/\[url\=(.*?)\](.*?)\[\/url\]/is'			=>	'<a href="$1" target="_blank" title="$2">$2</a>',
                '/\[url\=(.*?)\](.*?)\[\/url\]/is' => '<a href="$1" target="_blank">$2</a>',
//          '/\[url\](.*?)\[\/url\]/is'					=>	'<a href="$1" target="_blank">$1</a>',
                '/\[url\](.*?)\[\/url\]/is' => '<a href="$1" target="_blank">$1</a>',
                '/\[url\](\s*)\[\/url\]/is' => '',
//			'/\[img\](.*?)\[\/img\]/is' 				=>  '<img src="$1" border="0"/>',
                "/\[s\](.+?)\[\/s\]/is" => '<s>\1</s>',
                "/\[s\](\s*)\[\/s\]/is" => '',
                //"/\[quote\](.+?)\[\/quote\]/is"				=>	'<blockquote>$1</blockquote>',
                //"/\[quote\](\s*)\[\/quote\]/is"				=>	'',

                "/\[quote\]/is" => '<div class="block_mess_sub">',
                "/\[\/quote\]/is" => '</div>',
                "/\[hr\]/is" => '<hr>',
                "/\[dotter\]/is" => '<div style=" border-top:1px dotter #ccc">',
                "/\[\/dotter\]/is" => '</div>',
            );

            $text = preg_replace(array_keys($arr_replace), $arr_replace, $text);
            //$text = preg_replace("\\[color=([^\\[]*)\\]([^\\[]*)\\[/color\\]", "<font color=\"\\1\">\\2</font>", $text);
            // $text = preg_replace("\\[alink=([^\\[]*)\\]([^\\[]*)\\[/alink\\]","<a href=\"\\1\" target=\"_blank\" title=\"Liên kết\">\\2</a>",$text);
            //$text = preg_replace("\\[alink=([^\\[]*)\\]([^\\[]*)\\[/alink\\]", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $text);
        }

        $emoticon_dir = ($direct ? WEB_ROOT : '') . 'style/images/emoticon/';

        $arr_replace = array(
            "/:x/is" => '<img alt=":x" title=":x" src="' . $emoticon_dir . 'envy.gif" />',
            "/:d/is" => '<img alt=":D" title=":D" src="' . $emoticon_dir . 'grin.gif" />',
            "/\\[clap\\]/is" => '<img alt="clap" title="clap" src="' . $emoticon_dir . 'clap.gif" />',
            "/:-\\*/is" => '<img alt=":-*" title=":-*" src="' . $emoticon_dir . 'love.gif" />',
            "/:\\+\\)/is" => '<img alt=":+)" title=":+)" src="' . $emoticon_dir . 'shy.gif" />',
            "/:-s/is" => '<img alt=":-S" title=":-S" src="' . $emoticon_dir . 'fear.gif" />',
            "/:-\\(/is" => '<img alt=":-(" title=":-(" src="' . $emoticon_dir . 'unhappy.gif" />',
            "/:-@/is" => '<img alt=":-@" title=":-@" src="' . $emoticon_dir . 'uplook.gif" />',
            "/:-&/is" => '<img alt=":-&" title=":-&" src="' . $emoticon_dir . 'puke.gif" />',
            "/:-\\//is" => '<img alt=":-/" title=":-/" src="' . $emoticon_dir . 'question.gif" />',
            "/:-o/is" => '<img alt=":-o" title=":-o" src="' . $emoticon_dir . 'shock.gif" />',
            "/I-\\)/is" => '<img alt="I-)" title="I-)" src="' . $emoticon_dir . 'sleepy.gif" />',
            "/\\[tea\\]/is" => '<img alt="[tea]" title="[tea]" src="' . $emoticon_dir . 'coffee.gif" />',
            "/:\\(\\(/is" => '<img alt=":((" title=":((" src="' . $emoticon_dir . 'cry.gif" />',
            "/:\\)/is" => '<img alt=":)" title=":)" src="' . $emoticon_dir . 'smile.gif" />',
            "/\\[-\\(/is" => '<img alt=":x" title=":x" src="' . $emoticon_dir . 'sweat.gif" />',
            "/X-\\(/is" => '<img alt=":x" title=":x" src="' . $emoticon_dir . 'anger.gif" />',
            "/:-bd/is" => '<img alt=":x" title=":x" src="' . $emoticon_dir . 'wub.gif" />',
            "/:hh/is" => '<img alt=":hh" title=":hh" src="' . $emoticon_dir . 'hamarneh.gif" />',
            "/\\[pig\\]/is" => '<img alt="[pig]" title="[pig]" src="' . $emoticon_dir . 'pig.gif" />',
            "/\\[shuai\\]/is" => '<img alt="[shuai]" title="[shuai]" src="' . $emoticon_dir . 'shuai.gif" />',
            "/\\[bad\\]/is" => '<img alt="[bad]" title="[bad]" src="' . $emoticon_dir . 'bad.gif" />',
            "/\\[smoke\\]/is" => '<img alt="[smoke]" title="[smoke]" src="' . $emoticon_dir . 'smoke.gif" />',
            "/\\[stupid\\]/is" => '<img alt="[stupid]" title="[stupid]" src="' . $emoticon_dir . 'stupid.gif" />',
            "/\\[ok\\]/is" => '<img alt="[ok]" title="[ok]" src="' . $emoticon_dir . 'ok.gif" />',
            "/\\[thumbdown\\]/is" => '<img alt="[thumbdown]" title="[thumbdown]" src="' . $emoticon_dir . 'thumbdown.gif" />',
            "/\\[kill\\]/is" => '<img alt="[kill]" title="[kill]" src="' . $emoticon_dir . 'kill.gif" />',
            "/\\[dog\\]/is" => '<img alt="[dog]" title="[dog]" src="' . $emoticon_dir . 'dog.gif" />',
            "/\\[cool\\]/is" => '<img alt="[cool]" title="[cool]" src="' . $emoticon_dir . 'cool.gif" />',
            "/\\[zan\\]/is" => '<img alt="[zan]" title="[zan]" src="' . $emoticon_dir . 'zan.gif" />',
        );

        $text = preg_replace(array_keys($arr_replace), $arr_replace, $text);

        unset($old_text);
        return $text;
        //        return self::BBCode($text);
    }

    static function BBCode($text) {
        $text = stripslashes($text);
        //  $text = htmlspecialchars($text);
        // $text = nl2br($text);

        $arr_replace = array(
            '/<b>(.+?)<\/b>/is' => '[b]$1[/b]',
            '/<i>(.+?)<\/i>/is' => '[i]$1[/i]',
            '/<u>(.+?)<\/u>/is' => '[u]$1[/u]',
            '/<a href="(.*?)" target="_blank">(.*?)<\/a>/is' => '[url=$1]$2[/url]',
            '/<a href="(.*?)">(.*?)<\/a>/is' => '[url=$1]$2[/url]',
            '/<s>(.+?)<\s>/is' => '[s]$1[/s]',
            '/<blockquote>(.+?)<\/blockquote>/is' => '[quote]$1[/quote]',
            '/<br \/>/is' => "\n",
            '/<br\/>/is' => "\n",
            '/<br>/is' => "\n",
            '/&lt;br&gt;/is' => "\n"
        );

        $text = preg_replace(array_keys($arr_replace), $arr_replace, $text);

        return $text;
    }
}
?>